package cn.school.controller.classManage;


import cn.school.classManage.entity.ClassCurriculumMain;
import cn.school.classManage.service.ClassCurriculumDetailService;
import cn.school.classManage.service.ClassCurriculumMainService;
import cn.school.classManage.vo.*;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.course.dao.CourseSessionsDao;
import cn.school.course.entity.CourseSessions;
import cn.school.sys.entity.TermTime;
import cn.school.sys.service.TermTimeService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/classInfo")
@Api(tags = "班级课表")
public class ClassCurriculumController {

    @Resource
    private ClassCurriculumDetailService classCurriculumDetailService;

    @Resource
    private ClassCurriculumMainService classCurriculumMainService;

    @Resource
    TermTimeService termTimeService;

    @Resource
    CourseSessionsDao courseSessionsDao;

    private  boolean lockByExcelImport = false;

    /**
     * 分页查询班级课表列表
     * @param vo
     * @return
     * @throws ApiException
     * @throws IOException
     */
    @PostMapping("/findCurriculumPageList")
    @ApiOperation("分页查询班级列表")
    public CommonResult<CommonPage<ClassCurriculumMainVO>> findCurriculumPageList(@RequestBody ClassCurriculumMainVO vo) throws ApiException, IOException {
        IPage<ClassCurriculumMainVO> pageList = classCurriculumMainService.findCurriculumPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }



    /**
     * 新增班级课程
     * @param vo
     * @return
     * @throws ApiException
     * @throws IOException
     */
    @PostMapping("/addOrUpdateCurriculum")
    @ApiOperation("新增班级课程")
    public CommonResult<Integer> addOrUpdateCurriculum(@RequestBody ClassCurriculumMain vo) throws ApiException {
        int num = classCurriculumMainService.addOrUpdateCurriculum(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除班级课程
     * @param vo
     * @return
     * @throws ApiException
     * @throws IOException
     */
    @PostMapping("/deleteCurriculum")
    @ApiOperation("删除班级课程")
    public CommonResult<Integer> deleteCurriculum(@RequestBody ClassCurriculumMain vo) throws ApiException {
        int num = classCurriculumMainService.deleteCurriculum(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 查询课表详情
     * @param vo
     * @return
     * @throws ApiException
     * @throws IOException
     */
    @PostMapping("/findDetail")
    @ApiOperation("查询课表详情")
    public CommonResult<ClassCurriculumMain> findDetail(@RequestBody ClassCurriculumMainVO vo) throws ApiException {
        ClassCurriculumMain main = classCurriculumMainService.findDetail(vo);
        return CommonResult.success(main);
    }
//    @PostMapping("/findUser")
//    @ApiOperation("查询个人课表详情")
//    public CommonResult<ClassCurriculumMain> findUser(@RequestBody ClassCurriculumMainVO vo) throws ApiException {
//        ClassCurriculumMain main = classCurriculumMainService.findDetailss(vo);
//        return CommonResult.success(main);
//    }


    @PostMapping("/findStudentCurriculumDetail")
    @ApiOperation("查询课表详情")
    public CommonResult<ClassCurriculumMainVO> findStudentCurriculumDetail(@RequestBody ClassCurriculumMainVO vo) throws ApiException {
        ClassCurriculumMainVO main = classCurriculumMainService.findStudentCurriculumDetail(vo);
        return CommonResult.success(main);
    }



    /**
     * 删除课表详情
     * @param vo
     * @return
     * @throws ApiException
     * @throws IOException
     */
    @PostMapping("/deleteDetail")
    @ApiOperation("删除课表详情")
    public CommonResult<Integer> deleteDetail(@RequestBody ClassCurriculumMainVO vo) throws ApiException {

        return CommonResult.failed();
    }



    @ApiOperation("importTemplate => 下载模板")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<ClassCurriculumImportVO> list = new ArrayList<>();
        LambdaQueryWrapper<CourseSessions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseSessions::getTenantId, SysUserUtils.getSysUser().getTenantId());
        List<CourseSessions> sessions = courseSessionsDao.selectList(wrapper);
        ClassCurriculumImportVO vo = null;
        for (CourseSessions session : sessions) {
            vo = new ClassCurriculumImportVO();
            vo.setSessionsId(session.getCoseCode());
            list.add(vo);
        }
        EasyExcel.write(response.getOutputStream(), ClassCurriculumImportVO.class).head(getHeader("")).sheet("班级课表").doWrite(list);


    }



    @ApiOperation("export => 按条件导出（不分页）")
    @GetMapping("/export")
    public CommonResult export(HttpServletResponse response, @RequestParam(required = false) Map<String,Object> map) throws IOException {
        ClassCurriculumMainVO vo = new ClassCurriculumMainVO();
        vo.setId(Long.valueOf(String.valueOf(map.get("id"))));
        ClassCurriculumMainVO mainVO = classCurriculumMainService.findStudentCurriculumDetail(vo);
        List<ClassCurriculumExportVO> allList = new ArrayList<>();
        ClassCurriculumExportVO exportVO = null;
        for (ClassCurriculumDetailVO classCurriculumDetailVO : mainVO.getDetailList()) {
            exportVO = new ClassCurriculumExportVO();
            BeanUtils.copyProperties(classCurriculumDetailVO, exportVO);
            allList.add(exportVO);

        }
        EasyExcel.write(response.getOutputStream(), ClassCurriculumImportVO.class).head(getHeader(mainVO.getClassName())).sheet("班级课表").doWrite(allList);
        return CommonResult.failed();

    }

    @ApiOperation("导入联系人")
    @PostMapping("/importData")
    public CommonResult<String> importCurriculumExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException, ApiException {

        try {
            return this.classCurriculumMainService.importCurriculumExcel(multipartFile.getInputStream());
        } catch (ApiException apiException) {
            throw apiException;
        } catch (Exception e) {
            throw new ApiException("导入教员用户失败，请联系管理员");
        } finally {

        }

    }


    private List<List<String>> getHeader(String className){
        List<TermTime> termTimeList = termTimeService.getTermTimeByDate();
        String termTimename = "";
        if(termTimeList!=null && termTimeList.size()>0){
            termTimename = termTimeList.get(0).getRemarks();
        }
        String first = "班级课表导入";
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head0.add(first);
        head0.add("班级名称");
        head0.add("节次");
        List<String> head1 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head1.add(first);
        head1.add(className);
        head1.add("星期一");
        List<String> head2 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head2.add(first);
        head2.add("学期");
        head2.add("星期二");
        List<String> head3 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head3.add(first);
        head3.add(termTimename);
        head3.add("星期三");
        List<String> head4 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head4.add(first);
        head4.add(termTimename);
        head4.add("星期四");
        List<String> head5 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head5.add(first);
        head5.add(termTimename);
        head5.add("星期五");
        List<String> head6 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head6.add(first);
        head6.add(termTimename);
        head6.add("星期六");
        List<String> head7 = new ArrayList<String>();
        //注意这里是表头的合并上下两行
        head7.add(first);
        head7.add(termTimename);
        head7.add("星期日");

        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        list.add(head6);
        list.add(head7);
        return list;
    }


}
